var STD_WIDTH=768;
var STD_HEIGHT=1024;
var STD_SIDEBAR_BUTTON_WIDTH=100;
var VIEW_MODE_NORMAL=0;
var VIEW_MODE_DIALOG=1;
var VIEW_MODE_BOOK=2;
var VIEW_MODE_BOOK_DIALOG=3;
var NAVBAR_POS_TOP=0;
var NAVBAR_POS_BOTTOM=1;
var NAVBAR_WIDTH=STD_WIDTH;
var NAVBAR_JUMPSTEP=3;
var NAVBAR_CLICKED_OPACITY=0.5;
var NAVBAR_BG_COLOR='#888888';
var MASK_OPACITY=0.35;

var slideshow='myslideshow';
var slides_container='myslide_container';
var slide_navigator='myslide_navigator';
var left_ctrl='left_control';
var right_ctrl='right_control';
var slide_inner_div='slide_inner_div';

var up_nav='myslide_up_navigator';
var down_nav='myslide_down_navigator';

var updown_navigator='myslide_updown_navigator';
var cls_control='myslide_control';
var cls_img_control='myslide_img_control';
var add_vert_panel='myslide_additional_vertical_panel';

var masker='myslide_mask';

var book_wrapper='book_wrapper';
var mybook_name='mybook';
var mybook_content='mybook_content';

var namelist={
	0:slideshow,
	1:slideshow,
	2:book_wrapper,
	3:book_wrapper,
};

var nav_buttons_bar='myslide_nav_buttons_bar';

var nav_preview_bar='myslide_nav_preview_bar';
var nav_preview_bar_container='myslide_nav_preview_bar_container';
var nav_preview_bar_container_inner=nav_preview_bar_container+'_inner';
var preview_slide_id='myslide_preview_slide_id_';
var previous_clicked_nav_btn;

var self_obj;
var self_opts;

var script_folder_path='';
var slide_info={};
var slide_chapter=[];
var slide_width=0,slide_height=0,slide_scale=0;
var curr_pos=0;
var curr_v_pos=0;
var prev_pos=0;
var prev_v_pos=0;
var num_slides=0;
var max_horz_slides=0;
var max_vert_slides=0;

var control_name='';
var view_mode=0;
var nav_pos=0;
var nav_jumbstep=0;
var nav_clk_opacity=1;
var nav_bgcolor=0;
var mask_opacity=0;
var sidebar_w=0;
var num_buttons=0;
var button_imgs=[];
var button_callbacks=[];
var button_img_wrapped=true;
var background_rect=[];/**
 						* the background-rect is just designing 
 						*/
var callback_function=$.Callbacks('unique');

/**
 * this is the myslide slideshow version 1.0 created by minhld
 * make sure you insert the jquery library before using.
 **/
(function($){
	$.fn.myslide=function(options){
		var settings=$.extend({
			/** properties */
			'ctrl-name':'myslide',
			'view-mode':VIEW_MODE_NORMAL,
			'mask-opacity':MASK_OPACITY,
			'button-num':'3',
			'button-imgs':['','',''],
			'button-funcs':[],
			'button-img-wrapped':'true',
			'navbar-pos':'top',
			'navbar_jumpstep':NAVBAR_JUMPSTEP,
			'navbar_clicked_opacity':NAVBAR_CLICKED_OPACITY,
			'navbar_bgcolor':NAVBAR_BG_COLOR,
			'sidebar-width':STD_SIDEBAR_BUTTON_WIDTH,
			'background-rect':[],		
						/** only used with booklet style */
			
			/** events */
			onstart:function(e){},
			onclose:function(e){},
		},options);
		
		self_obj=this;
		self_opts=options;
		
		control_name=settings['ctrl-name'];
		view_mode=settings['view-mode'];
		nav_pos=settings['navbar-pos']=='top'?
				NAVBAR_POS_TOP:NAVBAR_POS_BOTTOM;
		nav_jumbstep=settings['navbar_jumpstep'];
		nav_clk_opacity=settings['navbar_clicked_opacity'];
		nav_bgcolor=settings['navbar_bgcolor'];
		sidebar_w=settings['sidebar-width'];
		mask_opacity=settings['mask-opacity'];
		num_buttons=settings['button-num'];
		button_imgs=settings['button-imgs'];
		button_callbacks=settings['button-funcs'];
		button_img_wrapped=settings['button-img-wrapped'];
		background_rect=settings['background-rect'];
			
		var slides=this.find('.slide');
		num_slides=slides.length;
		
		if (num_slides==0){
			alert('the '+control_name+' has no slide/page. '+
				'it will be closed now.');
			$(this).remove();
			return;
		}
		
		/*
		for (var i=0;i<num_slides;i++){
			slide_info[i]={};
		}
		*/
		
		if (view_mode!=VIEW_MODE_NORMAL&&
				view_mode!=VIEW_MODE_DIALOG){
			load_ex_js('otherslide.js','',startup_otherslide_from_host);
			return self_obj;
		}
		
		script_folder_path=load_js();

		var w=$('#'+slides_container).width();
		var h=$('#'+slides_container).height();
		slide_width=w;
		slide_height=h;
		slide_scale=w/h;
		
		$('.'+cls_control).css({
	    	'height':h,
	    });

		config_slideshow(w,h);

		var chap_idx=0,
			chap_name='',
			chap_hs_idx=0,
			chap_vs_idx=0;
		$.each(slides,function(k,v){
			chap_idx=parseInt($(v).attr('ch-idx'));
			chap_hs_idx=$(v).attr('h-idx');
			chap_vs_idx=$(v).attr('v-idx');

			if (slide_chapter[chap_idx]==undefined){
				slide_chapter[chap_idx]={
					'chap-idx':chap_idx,
					'chap-name':chap_name,
					'slides':{},
					'num-slides':0,
					'max-hidx':0,
					'max-vidx':0,
				};
				
			}
			
			if (slide_chapter[chap_idx]['slides'][chap_hs_idx]==undefined){
				slide_chapter[chap_idx]['slides'][chap_hs_idx]=[];
			}
			slide_chapter[chap_idx]['slides'][chap_hs_idx][chap_vs_idx]={
				'x':$(v).attr('h-idx'),
				'y':$(v).attr('v-idx'),
				'content':$(v),
			};
			
			slide_chapter[chap_idx]['num-slides']=
				parseInt(slide_chapter[chap_idx]['num-slides'])+1;
			
			
			if (parseInt(slide_chapter[chap_idx]['max-hidx'])<chap_hs_idx){
				slide_chapter[chap_idx]['max-hidx']=chap_hs_idx;
			}
			
			if (parseInt(slide_chapter[chap_idx]['max-vidx'])<chap_vs_idx){
				slide_chapter[chap_idx]['max-vidx']=chap_vs_idx;
			}

		});		
		
		var hidx=0,vidx=0,chap=null,slide_data=null;
		for (var i=0;i<slide_chapter.length;i++){
			if (slide_chapter[i]==undefined)
				continue;
			
			chap=slide_chapter[i];
			for (var j=0;j<=parseInt(chap['max-hidx']);j++){
				if (chap['slides'][j]==undefined)
					continue;
				
				if (slide_info[hidx]==undefined){
					slide_info[hidx]=[];
				}
				
				vidx=0;
				for (var k=0;k<=parseInt(chap['max-vidx']);k++){
					if (chap['slides'][j][k]==undefined)
						continue;
					
					slide_data=chap['slides'][j][k]['content'];
					slide_info[hidx][vidx]={
						'bg':$(slide_data).attr('bg'),
						'id':$(slide_data).attr('sid')!=undefined?
										$(slide_data).attr('sid'):'',
						'data':$(slide_data).attr('sdata')!=undefined?
										$(slide_data).attr('sdata'):'',
						'ch-idx':slide_chapter[i]['chap-idx'],
						'ch-name':slide_chapter[i]['chap-name'],
						'h-idx':j,
						'v-idx':k,
						'tag':$(slide_data),
					};
					
					vidx++;
				}
				
				if (slide_info[hidx][0]['v-mems']==undefined){
					slide_info[hidx][0]['v-mems']=vidx-1;
				}				
				
				if (max_vert_slides<vidx)
					max_vert_slides=vidx;
				
				hidx++;
			}
		}
		
		max_horz_slides=hidx;
		max_vert_slides;
		
		/*
		$.each(slides,function(k,v){
			chidx=parseInt($(v).attr('ch-idx'));
			hidx=parseInt($(v).attr('h-idx'));
			vidx=parseInt($(v).attr('v-idx'));
			slide_info[chidx][hidx][vidx]={
				'bg':$(v).attr('bg'),
				'id':$(v).attr('sid')!=undefined?
								$(v).attr('sid'):'',
				'data':$(v).attr('sdata')!=undefined?
								$(v).attr('sdata'):'',
				'ch-idx':chidx,
				'h-idx':hidx,
				'v-idx':vidx,
				'tag':$(v).clone(),
			};
			if (max_vert_slides<vidx){
				max_vert_slides=vidx;
			}
			
			if (max_horz_slides<hidx){
				max_horz_slides=hidx;
			}
			
			if (slide_info[hidx][0]==undefined){
				slide_info[hidx][0]={
					'v-mems':0,
				};
				
			}else if (slide_info[hidx][0]['v-mems']==null){
				slide_info[hidx][0]['v-mems']=0;
			}else{
				slide_info[hidx][0]['v-mems']=
					parseInt(slide_info[hidx][0]['v-mems'])+1;
			}
			$(v).remove();
		});
		
		max_horz_slides++;
		max_vert_slides++;
		*/
		
		config_slide_container();
		
		/*
	    slides.wrapAll('<div id="'+slide_inner_div+'"></div>')
		.css({
	  		'float':'left',
	        'width':w,
	        'height':h,
	  	});
	    $('#'+slide_inner_div).css({
	    	'width':w*num_slides,
	    	'height':h*max_vert_slides,
	    });
	    */
		
	    check_callback_functions();
	    config_navigator_bar();
	    
	    $('#'+slideshow)
      	.prepend(''+
      		'<div class="'+cls_control+'" style="left:0;">'+
      		'	<img id="'+left_ctrl+'" src="left.png" class="'+cls_img_control+'">'+
      		'</div>')
    	.append(''+
    		'<div class="'+cls_control+'" style="right:0;">'+
      		'	<img id="'+right_ctrl+'" src="right.png" class="'+cls_img_control+'">'+
    		'</div>');
	    
		var b_ctrl_pos=$('.'+cls_img_control).position();
		var b_ctrl_w=$('.'+cls_control).width()-b_ctrl_pos.left*2;
	    var b_ctrl_h=(h/w)*b_ctrl_w;
	    
	    $('#'+left_ctrl).css({
	    	'top':(h-b_ctrl_h)/2,
	    	'width':b_ctrl_w,
	    	'height':b_ctrl_h,
	    });
	    $('#'+right_ctrl).css({
	    	'top':(h-b_ctrl_h)/2,
	    	'width':b_ctrl_w,
	    	'height':b_ctrl_h,
	    });
	    
	    manage_controls(curr_pos);

	    $('.'+cls_img_control)
	      	.bind('click',function(){
	      		manage_sliding($(this).attr('id'));
	      	});
	    
	    if (view_mode==VIEW_MODE_DIALOG){
	    	add_n_config_mask();
	    }
	    
		this.get_current_slide_info=function(){
			return slide_info[curr_pos][curr_v_pos]['data'];
		};
		
		this.goto_slide=function(f,v){
			open_slide_by_field(f,v);
		};
		
		this.close=function(){
			remove_all();
		};
		
		open_slide(0);
		
		return this;
	};
})(jQuery);

function remove_all(){
	var name=namelist[view_mode];
	$('#'+name).remove();
	
	if (view_mode==VIEW_MODE_DIALOG||
			view_mode==VIEW_MODE_BOOK_DIALOG){
		$('#'+masker).remove();
	}
}

function default_callback(){
	alert('please implement for this function');
}

function check_callback_functions(){
	if (num_buttons>button_callbacks.length){
		for (var i=button_callbacks.length-1;
				i<num_buttons;i++){
			button_callbacks[i]=default_callback;
		}
	}
}

function add_corner_effects(){
	if (nav_pos==NAVBAR_POS_TOP){
		$('#'+slides_container).corner('bottom');
		$('#'+slide_navigator).corner('top');
	}else if (nav_pos==NAVBAR_POS_BOTTOM){
		$('#'+slides_container).corner('top');
		$('#'+slide_navigator).corner('bottom');
	}
		
	$('#'+left_ctrl).corner();
	$('#'+right_ctrl).corner();
	
	$('#'+nav_preview_bar).corner(10);
}

function add_n_config_mask(){
	var ctrl_name=namelist[view_mode]; 
	
	$('body').append(''+
	'<div id="'+masker+'" style="top:0;height:0;">'+
	'</div>'+
	'');
	$('#'+masker).css({
		'top':0,
		'left':0,
		'width':$(document).width(),
		'height':$(document).height(),
		'position':'absolute',
		'opacity':mask_opacity,
		'background':'#000000',
		'z-index':5000,
	});

	$(window).resize(function(){
		/*
		console.log($(document).width()+','+$(document).height());
		*/
		$('#'+masker).width($(document).width());
		$('#'+masker).height($(document).height());
		
		$('#'+ctrl_name).css({
			'top':0,
			'left':($(window).width()-$('#'+ctrl_name).width())/2,
		});
		
	});
	
	$('#'+masker).click(function(){
		if (self_opts.onclose!=undefined)
			self_opts.onclose.call(self_obj);
		remove_all();
	});
}

function config_slideshow(w,h){
	var ss_w=w+sidebar_w*2;
	var ss_h=h;
	if (view_mode==VIEW_MODE_DIALOG){
		$('#'+slideshow).css({
			'top':10,
			'left':($(window).width()-ss_w)/2,
			'width':ss_w,
			'height':ss_h,
			'background':'transparent',
			'position':'absolute',
			'z-index':5001,
		});
	}else if (view_mode==VIEW_MODE_NORMAL){
		$('#'+slideshow).css({
			'margin':'0 auto',
			'width':ss_w,
			'height':ss_h,
			'background':'transparent',
			'position':'relative',
		});
	}
}

function config_slide_container(){
	$('#'+slides_container).css({
	  	'margin':'0 auto',
  		'overflow':'auto',
  		'position':'relative',
	});
	$('#'+slides_container).css('overflow','hidden');
	$('#'+slides_container).append(
		'<div id="'+up_nav+'" class="'+updown_navigator+'" >'+
		'	<img src="'+script_folder_path+'up-arrow.png'+'">'+
		'</div>');
	$('#'+up_nav).css({
		'left':slide_width/2-parseInt($('#'+up_nav).css('width')),
		'top':'0px',
	}).mouseover(function(){
		$('#'+up_nav).animate({'opacity':1},200);
	}).mouseout(function(){
		$('#'+up_nav).animate({'opacity':0.3},200);
	}).click(function(){
		manage_v_sliding($(this).attr('id'));
	});
	
	$('#'+slides_container).append(
		'<div id="'+down_nav+'" class="'+updown_navigator+'" >'+
		'	<img src="'+script_folder_path+'down-arrow.png'+'">'+
		'</div>');
	
	$('#'+down_nav).css({
		'left':slide_width/2-parseInt($('#'+down_nav).css('width'))/2,
		'top':slide_height-parseInt($('#'+down_nav).css('height')),
	}).mouseover(function(){
		$('#'+down_nav).animate({'opacity':1},200);
	}).mouseout(function(){
		$('#'+down_nav).animate({'opacity':0.3},200);
	}).click(function(){
		manage_v_sliding($(this).attr('id'));
	});
	
	$('#'+slides_container).append(
			$('<div id="'+slide_inner_div+'"></div>').css({
		    	'position':'relative',
				'width':slide_width*(max_horz_slides+1),
		    	'height':slide_height*(max_vert_slides+1),
			}));
	
	var slide_obj=null;
	for (var i=0;i<max_horz_slides;i++){
		for (var j=0;j<max_vert_slides;j++){
			if (slide_info[i][j]==null||
					typeof(slide_info[i][j])===undefined)
				continue;
			slide_obj=slide_info[i][j]['tag'];
			$('#'+slide_inner_div).append(
				slide_obj.css({
					'position':'absolute',
					'float':'left',
					'top':j*slide_height,
					'left':i*slide_width,
					'width':slide_width,
					'height':slide_height,
				})
				
				/*
				$(slide_obj).css({
					'position':'absolute',
					'float':'left',
					'top':parseInt(slide_info[i][j]['v-idx'])*slide_height,
					'left':parseInt(slide_info[i][j]['h-idx'])*slide_width,
					'width':slide_width,
					'height':slide_height,
				})
				*/
			);
		}
	}

	$.each(this.find('.slide'),function(k,v){
		$(v).remove();
	});
}

function config_navigator_bar(){
    var nav_button_id='myslide_nav_button_id_';
    var nav_preview_button_left='myslide_prevbtn_left';
    var nav_preview_button_right='myslide_prevbtn_right';
    
    var prevbtn_size={width:10,height:20};
    var scale_left=10;
    
	if (nav_pos==NAVBAR_POS_TOP)
    	$('#'+slideshow).prepend('<div id="'+slide_navigator+'"></div>');
    else if (nav_pos==NAVBAR_POS_BOTTOM)
    	$('#'+slideshow).append('<div id="'+slide_navigator+'"></div>');
    
    $('#'+slide_navigator).width($('#'+slides_container).width());
    $('#'+slide_navigator).css({
    	'margin':'0 auto',
    	'position':'relative',
    	'z-index':10000,
    });
    
    $('#'+slide_navigator).append('<div id="'+nav_buttons_bar+'"></div>');
    var margin_left=0;
    var navbar_button_size=$('#'+slide_navigator).height()-20;
    
    for (var i=0;i<num_buttons;i++){
    	margin_left=scale_left+i*(5+navbar_button_size);
    	$('#'+nav_buttons_bar).append(
    		'<img id="'+nav_button_id+i+'" src="'+button_imgs[i]+'"> '+
    		'');
    	$('#'+nav_button_id+i).css({
    		/*
    		'width':navbar_button_size,
    		'height':navbar_button_size,
    		*/
    		'top':scale_left+'px',
    		'margin-left':margin_left,
    		'position':'absolute',
    		'border':'0',
    	});
  	
    	if (button_img_wrapped){
        	$('#'+nav_button_id+i).css({
        		'width':navbar_button_size,
        		'height':navbar_button_size,
        	});
    	}
    	
    	$('#'+nav_button_id+i).click(function(){
    		var e=$(this).attr('id').split('_');
    		var idx=parseInt(e[e.length-1]);
    		callback_function.add(button_callbacks[idx]);
    		callback_function.fire();
    		callback_function.remove(button_callbacks[idx]);
    	});
    }
    
    var nav_prev_bar_left=margin_left+
    			navbar_button_size+scale_left*2;
   
    /**
     * draw the preview panel - including left and right navigation buttons
     * and the preview slide container. 
     */
    
    
    var nav_prev_bar_w=$('#'+slide_navigator).width()-
    			nav_prev_bar_left-scale_left*2;
    /*
    var nav_prev_bar_w=100;
    */
    $('#'+slide_navigator).append('<div id="'+nav_preview_bar+'"></div>');
    $('#'+nav_preview_bar).css({
    	'top':scale_left+'px',
    	'margin-left':nav_prev_bar_left,
    	'width':nav_prev_bar_w+'px',
    	'height':navbar_button_size+'px',
    	'background':nav_bgcolor,
    	'position':'absolute',
    });
    
    $('#'+nav_preview_bar).append('<div>'+
    	'<img id="'+nav_preview_button_left+'" '+
    	'	src="'+script_folder_path+'left-arrow.png" '+
    	'	style="position:absolute;'+
    	'		top:'+(navbar_button_size-prevbtn_size.height)/2+'px;'+
    	'		left:0px;">'+
    	'</div>');
    
    var prevbar_container_w=nav_prev_bar_w-prevbtn_size.width*2;
    $('#'+nav_preview_bar).append('<div id="'+nav_preview_bar_container+'" '+
    	'	style="position:relative;'+
    	'		top:0px;'+
    	'		left:'+prevbtn_size.width+'px;'+
    	'		width:'+prevbar_container_w+'px;'+
    	'		height:'+navbar_button_size+'px;'+
    	'		overflow:hidden;'+
    	'	">'+
    	'</div>');
    
    $('#'+nav_preview_bar).append('<div>'+
    	'<img id="'+nav_preview_button_right+'" '+
    	'	src="'+script_folder_path+'right-arrow.png" '+
    	'	style="position:absolute;'+
    	'		top:'+(navbar_button_size-prevbtn_size.height)/2+'px;'+
    	'		left:'+(nav_prev_bar_w-prevbtn_size.width)+'px;">'+
    	'</div>');
    
    /**
     * draw the list of preview slides
     */
    
    var prev_slideimg_h=navbar_button_size-6;
    var prev_slideimg_w=slide_scale*prev_slideimg_h;
    var prev_slideimg_inner_w=(prev_slideimg_w+6)*(max_horz_slides);
    
    $('#'+nav_preview_bar_container).
    	append('<div id="'+nav_preview_bar_container_inner+'"></div>');
    $('#'+nav_preview_bar_container_inner).css({
    	'width':prev_slideimg_inner_w,
    	'height':navbar_button_size,
    	'position':'absolute',
    });

    var num_vmems=0;
    for (var i=0;i<max_horz_slides;i++){
    	var preview_id=preview_slide_id+i;
    	if (i==0){
    		previous_clicked_nav_btn=preview_id;
    		opacity=1;
    	}else{
    		opacity=nav_clk_opacity;
    	}
    	
    	num_vmems=slide_info[i][0]['v-mems']!=null?
    			slide_info[i][0]['v-mems']:0;
    	
    	$('#'+nav_preview_bar_container_inner).
    	append(''+
    		'<div style="'+
    		'		float:left;'+
    		'		position:relative;'+
    		'		width:'+(prev_slideimg_w+6)+'px;'+
    		'		height:'+(navbar_button_size)+'px;">'+
    		'	<img id="'+preview_id+'" src="'+slide_info[i][0]['bg']+'" '+
    		'		style="top:3px;margin-left:3px;margin-right:3px;'+
    		'			width:'+prev_slideimg_w+'px;'+
    		'			height:'+prev_slideimg_h+'px;'+
    		'			opacity:'+opacity+';'+
    		'			position:absolute;"'+
    		'		h-idx="'+i+'" v-idx="0">'+
    		(num_vmems>0?
    		'	<div style="background:#333;'+
    		'				position:absolute;'+
    		'				top:'+(prev_slideimg_w+3-1)+'px;'+
    		'				left:'+(prev_slideimg_w-20+3)+'px;'+
    		'				width:20px;height:15px;'+
    		'				font-size:9px;color:#fff;'+
    		'				font-family:tahoma;cursor:default;" '+
    		'		align="center"'+
    		'		>'+
    		''+num_vmems+'</div>':
    		'')+
    		'</div>');
    	
    	$('#'+preview_id).click(function(){
    		var id=$(this).attr('id');
    		var e=id.split('_');
    		var slide_idx=parseInt(e[e.length-1]);
    		var button_changed=previous_clicked_nav_btn!=id;
    		/*
    		var hidx=$(this).attr('h-idx');
    		*/
    		if (button_changed||(!button_changed&&curr_v_pos!=0)){
	    		open_slide(slide_idx);
    		}

    		show_vert_slide_panel(id,slide_idx,
    			parseInt(slide_info[slide_idx][0]['v-mems']),
    			prev_slideimg_w,prev_slideimg_h,
    			button_changed);
    	
    	}).mouseover(function(){
    		$(this).css({
    			'border':'1px solid #fff',
    		});
//    		show_vert_slide_panel('on',$(this).attr('id'),5);
    	}).mouseout(function(){
    		$(this).css({
    			'border':'0px',
    		});
//    		show_vert_slide_panel('off');
    	});
    }
    
    /**
     * add the additional vertical panel for vertical slides 
     */
	$('#'+slide_navigator).append(
		$('<div id="'+add_vert_panel+'"></div>').
			css({
				'display':'none',
				'position':'absolute',
				'z-index':20000,
				'background':'transparent',
			})
	);
	
    /**
     * add dragging effects for inner preview slides
     */
    add_dragging_effects();
    
    /** navigation buttons' (left&right) actions */
    
    $('#'+nav_preview_button_left).click(function(){
    	if(prev_slideimg_inner_w>prevbar_container_w){
	    	var step_w=nav_jumbstep*(prev_slideimg_w+6);
	    	var offs_l=$('#'+nav_preview_bar_container_inner).position().left;
	    	if (prev_slideimg_inner_w-(-offs_l+step_w)>prevbar_container_w){
	    		$('#'+nav_preview_bar_container_inner).animate({
	    			'left':offs_l-step_w,
	    		},500);
	    	}else{
	    		$('#'+nav_preview_bar_container_inner).animate({
	    			'left':prevbar_container_w-prev_slideimg_inner_w,
	    						/** 20 - width of navigation button*/
	    		},500);
	    	}
    	}
    });
    
    $('#'+nav_preview_button_right).click(function(){
    	if(prev_slideimg_inner_w>prevbar_container_w){
	    	var step_w=nav_jumbstep*(prev_slideimg_w+6);
	    	var offs_l=$('#'+nav_preview_bar_container_inner).position().left;
    		if (prev_slideimg_inner_w-(-offs_l-step_w)>prevbar_container_w&&
    				offs_l+step_w<0){
    			$('#'+nav_preview_bar_container_inner).animate({
	    			'left':offs_l+step_w,
	    		},500);
    		}else{
	    		$('#'+nav_preview_bar_container_inner).animate({
	    			'left':0,
	    		},500);
    		}
    	}
    });
}

function add_dragging_effects(){
	var inner_currpos=$('#'+
			nav_preview_bar_container_inner).offset();
	var inner_w=$('#'+
			nav_preview_bar_container_inner).width();
	var container_w=$('#'+
			nav_preview_bar_container).width();
	
	if (inner_w<=container_w){
		$('#'+nav_preview_bar_container_inner).
			draggable({containment:'parent'});
	}else{
		$('#'+nav_preview_bar_container_inner).
			draggable({
				containment:
					[inner_currpos.left+(container_w-inner_w),
					 inner_currpos.top,
				     inner_currpos.left,
				     inner_currpos.top],
			});
	}

	/*
	drag(function(ev,dd){
		if(inner_w<container_w&&
			dd.offsetX-inner_currpos.left>=0&&
			(dd.offsetX-inner_currpos.left)+
					inner_w<=container_w){
			$(this).css({
				left:dd.offsetX-inner_currpos.left,
			});
		}else if(inner_w>=container_w&&
			dd.offsetX-inner_currpos.left<=0&&
			(dd.offsetX-inner_currpos.left)+
					inner_w>=container_w){
			$(this).css({
				left:dd.offsetX-inner_currpos.left,
			});
		}
	});
	*/
}

function show_vert_slide_panel(id,idx,
			num_v_slides,sw,sh,btn_chg){
	if (num_v_slides==0){
		$('#'+add_vert_panel).toggle(false);
		return;
	}
	
	var off_dist=
		$('#'+nav_preview_bar_container_inner).offset().left-
		$('#'+nav_buttons_bar).offset().left;
	
	$('#'+add_vert_panel).css({
		'width':(sw)+'px',
		'height':(num_v_slides*sh)+'px',
		'top':(sh+20-3)+'px',
		'left':(off_dist+(sw+6)*idx+3)+'px',
		
	});
	
	$('#'+add_vert_panel).empty();
	for (var i=1;i<num_v_slides+1;i++){
		$('#'+add_vert_panel).append(
			$('<img src="'+slide_info[idx][i]['bg']+'"'+
			'	width="'+(sw)+'px" height="'+(sh)+'px"'+
			'	style="opacity:0.6;"'+
			'	h-idx="'+idx+'" v-idx="'+i+'" '+
			'>').mouseover(function(){
				$(this).css({
					'border':'1px solid #fff',
				});
			}).mouseout(function(){
				$(this).css({
					'border':'0px',
				});
			}).click(function(){
				var hidx=parseInt($(this).attr('h-idx'));
				var vidx=parseInt($(this).attr('v-idx'));
				$('#'+add_vert_panel).fadeToggle(300);
				open_v_slide(hidx,vidx);
			})
		);
	}
	
	if (btn_chg){
		$('#'+add_vert_panel).toggle(btn_chg);
	}else{
		$('#'+add_vert_panel).fadeToggle(300);
	}
}

function open_slide_by_field(f,v){
	var hidx=-1,vidx=-1;
	var e;
	for (var i=0;i<num_slides;i++){
		if (slide_info[i]==undefined)
			continue;
		
		for (var j=0;j<slide_info[i].length;j++){
			e=jQuery.parseJSON(slide_info[i][j]['data']);
			if (e[f]==v){
				hidx=i;
				vidx=j;
				break;
			}
		}
	}
	
	if (hidx>=0&&vidx>=0)
		open_v_slide(hidx,vidx);
}

function open_slide(pos){
	curr_pos=pos;
	prev_pos=curr_pos-1;
	curr_v_pos=0;
	
	manage_controls(curr_pos,prev_pos);
	
	$('#'+slide_inner_div).css({
		'marginLeft':slide_width*(-curr_pos),
		'marginTop':0,
	});
	
	manage_updown_buttons(pos);
	manage_preview_buttons(pos);
}

function open_v_slide(pos,v_pos){
	curr_pos=pos;
	curr_v_pos=v_pos;
	var num_v_slides=parseInt(
			slide_info[curr_pos][0]['v-mems']);
	
	manage_v_controls(curr_pos,curr_v_pos,
			curr_v_pos-1,num_v_slides);
	
	$('#'+slide_inner_div).css({
		'marginLeft':slide_width*(-curr_pos),
		'marginTop':slide_height*(-curr_v_pos),
	});
	

}

function manage_v_sliding(control){
	prev_v_pos=curr_v_pos;
	curr_v_pos=(control==down_nav)?
			curr_v_pos+1:curr_v_pos-1;
	var num_v_slides=parseInt(
			slide_info[curr_pos][0]['v-mems']);
	
	manage_v_controls(curr_pos,curr_v_pos,
			prev_v_pos,num_v_slides);
	
	$('#'+slide_inner_div).animate({
		'marginLeft':slide_width*(-curr_pos),
		'marginTop':slide_height*(-curr_v_pos),
	});
}

function manage_sliding(control){
	prev_pos=curr_pos;
	curr_v_pos=0;
	curr_pos=(control==right_ctrl)?
			curr_pos+1:curr_pos-1;
  
	manage_controls(curr_pos,prev_pos);
	
	$('#'+slide_inner_div).animate({
		'marginLeft':slide_width*(-curr_pos),
		'marginTop':0,//slide_height*(-curr_v_pos),
	},500);
	
	manage_updown_buttons(curr_pos);
	manage_preview_buttons(curr_pos);
}

function manage_preview_buttons(pos){
	var curr_navbtn_id=preview_slide_id+pos;
	$('#'+curr_navbtn_id).css({
		'opacity':1,
	});
	if (curr_navbtn_id!=previous_clicked_nav_btn){
		$('#'+previous_clicked_nav_btn).css({
			'opacity':nav_clk_opacity,
		});
	}
	previous_clicked_nav_btn=curr_navbtn_id;
}

function manage_updown_buttons(pos){
	var num_mems=parseInt(slide_info[pos][0]['v-mems']);
	if (num_mems==0){
		$('#'+up_nav).css('opacity',0);
		$('#'+up_nav).hide();
		$('#'+down_nav).css('opacity',0);
		$('#'+down_nav).hide();
	}else{
		$('#'+up_nav).css('opacity',0.3);
		$('#'+up_nav).hide();
		$('#'+down_nav).css('opacity',0.3);
		$('#'+down_nav).show();
	}
}

function manage_v_controls(pos,vpos,vprev_pos,max_v_slides){
	if(vpos==0){ 
		if (vprev_pos==1)
			$('#'+up_nav).fadeOut('fast');
		else 
			$('#'+up_nav).hide();
	}else{ 
		$('#'+up_nav).fadeIn('fast'); 
	}
	
	if(vpos==max_v_slides){ 
		$('#'+down_nav).fadeOut('fast'); 
	}else{ 
		$('#'+down_nav).fadeIn('fast'); 
	}

}

function manage_controls(pos,prevp){
	if(pos==0){ 
		if (prevp==1)
			$('#'+left_ctrl).fadeOut('fast');
		else 
			$('#'+left_ctrl).hide();
	}else{ 
		$('#'+left_ctrl).fadeIn('fast'); 
	}
	
	if(pos==max_horz_slides-1){ 
		$('#'+right_ctrl).fadeOut('fast'); 
	}else{ 
		$('#'+right_ctrl).fadeIn('fast'); 
	}
	
	$('#'+left_ctrl).attr('src',
			pos>=1?slide_info[pos-1][0]['bg']:slide_info[0][0]['bg']);
	$('#'+right_ctrl).attr('src',
			pos<max_horz_slides-1?slide_info[pos+1][0]['bg']:
							slide_info[max_horz_slides-1][0]['bg']);
}

function startup_otherslide_from_host(){
	startup_otherslide();
}

/**
 * load the javascript files on the fly
 */
function load_js(){
    var scripts = document.getElementsByTagName("script");
    var src='';
    $.each(scripts,function(){
    	if (this.src.indexOf('myslide.js')>=0){
    		src=this.src;
    		return;
    	}
    });
    var end_offs=src.length-'myslide.js'.length;
    src=src.substring(0, end_offs);
    
    var j_corner_js=src+'jquery.corner.js';
    load_script(j_corner_js,add_corner_effects);
    
    return src; 
}

function load_ex_js(js,add_path,callback){
    var scripts = document.getElementsByTagName("script");
    var src='';
    $.each(scripts,function(){
    	if (this.src.indexOf('myslide.js')>=0){
    		src=this.src;
    		return;
    	}
    });
    var end_offs=src.length-'myslide.js'.length;
    src=src.substring(0, end_offs);
    
    var js_file=src+add_path+js;
    load_script(js_file,callback);
    
    return src; 
}

function load_script(url, callback){
	
	var script = document.createElement('script');
	script.type = 'text/javascript';
	

	if (script.readyState){  /** IE */
		script.onreadystatechange=function(){
			if (script.readyState=="loaded"||
					script.readyState=="complete"){
                script.onreadystatechange=null;
                callback();
            }
        };
    }else{
    	script.onload=function(){
            callback();
        };
    }

	/*
	script.onreadystatechange = callback;
	script.onload = callback;
	
	head.appendChild(script);
	*/
	
	var head = document.getElementsByTagName('head')[0];
	script.src = url;
	head.appendChild(script);
}

function load_css(css,add_path){
    var scripts = document.getElementsByTagName("script");
    var src='';
    $.each(scripts,function(){
    	if (this.src.indexOf('myslide.js')>=0){
    		src=this.src;
    		return;
    	}
    });
    var end_offs=src.length-'myslide.js'.length;
    src=src.substring(0, end_offs);
    
    var css_f=src+add_path+css;
    if (document.createStyleSheet){
        document.createStyleSheet(css_f);
    }else{
		$('<link/>',{
			   rel:'stylesheet',
			   type:'text/css',
			   href:css_f
		}).appendTo('head');
    }
    return src;
}